﻿package com.sohu.sns.avatar {
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;

	public class DragBox extends Sprite {
		private var gripper : Sprite;
		private var selfHeight : Number;
		private var initWidth : Number;
		private var isDragging : Boolean = false;
		private var right : Number;
		private var bottom : Number;
		private var isResizing : Boolean = false;
		private var bg : Sprite;
		private var offX : Number = 0;
		private var offY : Number = 0;
		private var initHeight : Number;
		private var selfWidth : Number;
		private var Grapper : Class;
		private var originalScale : Number;
		private var dragBoundary : Rectangle;
		public static var MOVE : String = "moving";
		public static var START_RESIZE : String = "startResize";
		public static var STOPMOVE : String = "stop";
		public static var RESIZE : String = "resizing";
		public static var START_MOVE : String = "startMove";

		public function DragBox(dragBoundary : Rectangle, selfWidth : Number = 100, selfHeight : Number = 100) {
			Grapper = DragBox_Grapper;
			bg = new Sprite();
			this.dragBoundary = dragBoundary;
			this.right = right;
			this.bottom = bottom;
			this.selfWidth = selfWidth;
			this.selfHeight = selfHeight;
			this.originalScale = selfWidth / selfHeight;
			this.initWidth = selfWidth;
			this.initHeight = selfHeight;
			addChild(this.bg);
			this.addEventListener(Event.ADDED_TO_STAGE, onStage);
		}

		private function onBgMouseOut(event : MouseEvent) : void {
			if (this.isDragging || this.isResizing) {
				return;
			}
			CustomCursor.getInstance().showNormal();
		}

		private function initListeners() : void {
			this.bg.addEventListener(MouseEvent.MOUSE_OVER, onBgMouseOver);
			this.bg.addEventListener(MouseEvent.MOUSE_OUT, onBgMouseOut);
			this.addEventListener(MouseEvent.MOUSE_DOWN, onBgMouseDown);
			this.addEventListener(MouseEvent.MOUSE_UP, onBgMouseUp);
			this.gripper.addEventListener(MouseEvent.MOUSE_OVER, onRectMouseOver);
			this.gripper.addEventListener(MouseEvent.MOUSE_OUT, onRectMouseOut);
			this.gripper.addEventListener(MouseEvent.MOUSE_DOWN, onRectDown);
			this.gripper.addEventListener(MouseEvent.MOUSE_UP, onRectUp);
		}

		public function resetBoundary(param1 : Rectangle) : void {
			this.dragBoundary = param1;
		}

		public function get miniLength() : Number {
			return this.initWidth > this.initHeight ? (initHeight) : (this.initWidth);
		}

		private function onRectDown(event : MouseEvent) : void {
			this.isResizing = true;
			CustomCursor.getInstance().showResize(DisplayObject(event.target));
			this.dispatchEvent(new Event(START_RESIZE));
			this.offX = event.target['width'] - event.target['mouseX'];
			this.offY = event.target['height'] - event.target['mouseY'];
			event.stopPropagation();
			stage.addEventListener(MouseEvent.MOUSE_MOVE, resizing);
			stage.addEventListener(MouseEvent.MOUSE_UP, onRectUp);
		}

		private function drawGripper() : void {
			this.gripper = new Grapper();
		}

		public function get minHeight() : Number {
			return this.initHeight;
		}

		public function resume(param1 : Number, param2 : Number) : void {
			this.resizeBox(param1, param2);
		}

		public function get miniWidth() : Number {
			return this.initWidth;
		}

		private function onBgMouseDown(event : MouseEvent) : void {
			this.isDragging = true;
			this.dispatchEvent(new Event(START_MOVE));
			CustomCursor.getInstance().showMove();
			var dragRange : Rectangle = new Rectangle(this.dragBoundary.x, this.dragBoundary.y, this.dragBoundary.width - this.width, this.dragBoundary.height - this.height);
			this.startDrag(false, dragRange);
			stage.addEventListener(MouseEvent.MOUSE_UP, dragStop);
			stage.addEventListener(MouseEvent.MOUSE_MOVE, onMove);
		}

		public function get box() : Sprite {
			return this.bg;
		}

		public function resize(param1 : Number) : void {
			if (this.initWidth > this.initHeight) {
				this.resizeBox(param1, param1 / this.originalScale);
			} else {
				this.resizeBox(param1 * this.originalScale, param1);
			}
		}

		private function onMove(event : Event) : void {
		}

		private function dragStop(event : MouseEvent) : void {
			if (!this.bg.hitTestPoint(event.stageX, event.stageY)) {
				CustomCursor.getInstance().showNormal();
			}
			this.stopDrag();
			stage.removeEventListener(MouseEvent.MOUSE_UP, dragStop);
			stage.removeEventListener(MouseEvent.MOUSE_MOVE, onMove);
			this.dispatchEvent(new Event(STOPMOVE));
			this.isDragging = false;
		}

		private function onRectUp(event : MouseEvent) : void {
			CustomCursor.getInstance().showNormal();
			stage.removeEventListener(MouseEvent.MOUSE_MOVE, resizing);
			stage.removeEventListener(MouseEvent.MOUSE_UP, onRectUp);
			this.dispatchEvent(new Event(STOPMOVE));
			this.isResizing = false;
		}

		private function onStage(event : Event) : void {
			this.removeEventListener(Event.ADDED_TO_STAGE, onStage);
			this.drawGripper();
			this.drawBox();
			this.gripper.x = this.width - this.gripper.width - 1;
			this.gripper.y = this.height - this.gripper.height - 1;
			this.addChild(this.gripper);
			this.initListeners();
			stage.addChild(CustomCursor.getInstance());
		}

		private function drawBox() : void {
			this.bg.graphics.clear();
			this.bg.graphics.beginFill(0, 0);
			this.bg.graphics.lineStyle(1, 0xFFFFFF);
			this.bg.graphics.drawRect(0, 0, this.selfWidth, this.selfHeight);
			this.bg.graphics.endFill();
		}

		public function get maxLength() : Number {
			return this.initWidth < this.initHeight ? (initHeight) : (this.initWidth);
		}

		private function onRectMouseOut(event : MouseEvent) : void {
			if (this.isResizing || this.isDragging) {
				return;
			}
			CustomCursor.getInstance().showNormal();
		}

		private function resizing(event : MouseEvent) : void {
			if (event.stageX > this.x && event.stageY > this.y) {
				this.resizeBox(this.mouseX, this.mouseY, this.offX, this.offY);
			}
		}

		private function onBgMouseOver(event : MouseEvent) : void {
			if (this.isResizing) {
				return;
			}
			CustomCursor.getInstance().showMove();
		}

		private function resizeBox(param1 : Number, param2 : Number, param3 : Number = 0, param4 : Number = 0) : void {
			var rightX : Number = this.dragBoundary.width + this.dragBoundary.x;
			var bottomY : Number = this.dragBoundary.height + this.dragBoundary.y;
			this.selfWidth = param1 + param3;
			this.selfHeight = param2 + param4;
			if (this.selfWidth <= 50 + param3) {
				this.selfWidth = 50 + param3;
			}
			if (this.selfHeight <= 50 + param4) {
				this.selfHeight = 50 + param3;
			}
			if (this.selfWidth > rightX - this.x) {
				this.selfWidth = rightX - this.x;
			}
			if (this.selfHeight > bottomY - this.y) {
				this.selfHeight = bottomY - this.y;
			}
			if (this.selfWidth < this.selfHeight * this.originalScale) {
				this.selfHeight = this.selfWidth / this.originalScale;
			} else {
				this.selfWidth = this.selfHeight * this.originalScale;
			}
			this.drawBox();
			this.gripper.x = this.selfWidth - this.gripper.width;
			this.gripper.y = this.selfHeight - this.gripper.height;
		}

		private function onBgMouseUp(event : MouseEvent) : void {
		}

		private function onRectMouseOver(event : MouseEvent) : void {
			if (this.isDragging) {
				return;
			}
			CustomCursor.getInstance().showResize(DisplayObject(event.target));
		}
	}
}
